import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./routes";
import store from "@/store/index.js";

Vue.use(VueRouter);

/**
* @param {Object} callback
* 页面隐藏动画
*/
function hide(callback,param){
    setTimeout(() => {
      var ele=document.getElementById('apparea');
        if(ele){
            const classList = ele.classList;
            classList.add('animation-leave')
            classList.remove('animation-show')
            setTimeout(() => {
              classList.remove('animation-leave')
                if(param)
                {
                    callback && callback(param);
                }else{
                    callback && callback();
                }
            }, 300);
        }else{
            if(param)
            {
                callback && callback(param);
            }else{
                callback && callback();
            }
        }
    },20)
}

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes: routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
           return savedPosition;
        } else {
           return {x: 0, y: 0};
        }
    }
});

router.beforeEach((to, from, next) => {
  hide(next,null)
  if (to.meta.title) {
      document.title = to.meta.title;
  }
});

router.afterEach((to, from, next) => {
   window.scrollTo(0, 0);
});

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
}
export default router;

